package teleradiologia.web;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;

public class Utils {

	public static final String utf8Convert(String utf8String) {
		byte[] bytes = new byte[utf8String.length()];
		for (int i = 0; i < utf8String.length(); i++) {
			bytes[i] = (byte) utf8String.charAt(i);
		}
		String result;
		try {
			result = new String(bytes, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			result = null;
		}
		return result;
	}

	public static final BufferedImage scaleImage(BufferedImage image, int width, int height) {

		BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		Graphics2D graphics = result.createGraphics();

		graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
		    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

		graphics.drawImage(image, 0, 0, width, height, null);

		graphics.dispose();

		return result;
	}

}
